/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.client.renderer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import locusts.common.World;

public class BackgroundRenderer {

    private static final Color FILL_COLOR = new Color(220,220,220);
    private static final Color LINE_COLOR = Color.black;
//    private final Image grass;
    private World world;

    public BackgroundRenderer(World world) {
        super();
        this.world = world;
//        Toolkit tk = Toolkit.getDefaultToolkit();
//        Image tmp = tk.createImage("res/grasstexture.jpg");
//        grass = tmp.getScaledInstance(300, 300, Image.SCALE_SMOOTH);

    }

    public void render(Graphics2D g) {
//        Rectangle2D bounds = g.getClipBounds();
        g.setColor(FILL_COLOR);
        g.fill(g.getClip());

//        g.setColor(new Color(32, 128, 32));
//        g.fill(bounds);
//
//        drawTiledImage(g, grass);
//        g.drawImage(createBackgroundImage(), 0, 0, null);
//            if (world != null)
        g.setColor(LINE_COLOR);
        drawWorldBoundry(g);
    }
    private void drawWorldBoundry(Graphics2D g) {
        final Color grid_colour = new Color(0, 0, 0, 64);
        final Stroke grid_stroke = new BasicStroke(8);
        Rectangle2D r = world.getBounds();
        double x0 = r.getMinX();
        double y0 = r.getMinY();
        double x1 = r.getMaxX();
        double y1 = r.getMaxY();
        int s = 10;
//            for (double x = x0; x <= x1; x += s) {
//                g.drawLine((int) x - s, (int) y0 - s, (int) x, (int) y0);
//                g.drawLine((int) x, (int) y1, (int) x + s, (int) y1 + s);
//            }
//            for (double y = y0; y <= y1; y += s) {
//                g.drawLine((int) x0 - s, (int) y - s, (int) x0, (int) y);
//                g.drawLine((int) x1, (int) y, (int) x1 + s, (int) y + s);
//            }
//            g.drawLine((int) x1, (int) y0 - s, (int) x1 + s, (int) y0);
//            g.drawLine((int) x0 - s, (int) y1, (int) x0, (int) y1 + s);
        g.setColor(grid_colour);
        g.setStroke(grid_stroke);
        g.draw(r);


        g.drawLine((int) x0, (int) (1 * (y1 - y0) / 4), (int) x1, (int) (1 *
                (y1 - y0) / 4));
        g.drawLine((int) x0, (int) (2 * (y1 - y0) / 4), (int) x1, (int) (2 *
                (y1 - y0) / 4));
        g.drawLine((int) x0, (int) (3 * (y1 - y0) / 4), (int) x1, (int) (3 *
                (y1 - y0) / 4));

        g.drawLine((int)(1 * (x1-x0) / 4), (int)y0, (int)(1 * (x1-x0) / 4), (int)y1);
        g.drawLine((int)(2 * (x1-x0) / 4), (int)y0, (int)(2 * (x1-x0) / 4), (int)y1);
        g.drawLine((int)(3 * (x1-x0) / 4), (int)y0, (int)(3 * (x1-x0) / 4), (int)y1);
    }

//
//    private BufferedImage createBackgroundImage() {
//        int width = 100;
//        int height = 100;
//        BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//        out.getGraphics().setClip(0, 0, width, height);
//        drawTiledImage((Graphics2D)out.getGraphics(), grass);
//        return out;
//    }
//
//
//
//    private void drawTiledImage(Graphics2D g, Image im) {
//        final int w = im.getWidth(null);
//        final int h = im.getHeight(null);
//        if (w < 1 || h < 1) return;
//        final Rectangle2D bounds = g.getClipBounds();
//
//        // expand bounds to start and end on a tile boundry
//        final int x0 = (int) (w * Math.floor(bounds.getMinX() / w));
//        final int y0 = (int) (h * Math.floor(bounds.getMinY() / h));
//        final int x1 = (int) (w * Math.ceil(bounds.getMaxX() / w));
//        final int y1 = (int) (h * Math.ceil(bounds.getMaxY() / h));
//
//        for (int x = x0; x < x1; x += w) {
//            for (int y = y0; y < y1; y += h) {
//                g.drawImage(im, x, y, null);
//            }
//        }
//    }

//    private static BufferedImage makeTiledImage(Image im, int width,
//            int height) {
//        System.out.println("\n\n\nRedrawing tiles background!\n\n\n");
//        final BufferedImage out = new BufferedImage(width, height,
//                BufferedImage.TYPE_INT_RGB);
//        Graphics2D g = (Graphics2D) out.getGraphics();
//        final int iw = im.getWidth(null) - 1;
//        final int ih = im.getHeight(null) - 1;
//
//        for (int x = 0; x < width; x += iw) {
//            for (int y = 0; y < height; y += ih) {
//                g.drawImage(im, x, y, null);
//            }
//        }
//        return out;
//    }

//    private void drawTileGrid(Graphics2D g, double w, double h) {
//        final Color grid_colour = Color.GRAY;
//        final Stroke grid_stroke = new BasicStroke(1);
//        final Rectangle2D bounds = g.getClipBounds();
//        // expand bounds to start and end on a tile boundry
//        final int x0 = (int) (w * Math.round(bounds.getMinX() / w - 1));
//        final int y0 = (int) (h * Math.round(bounds.getMinY() / h - 1));
//        final int x1 = (int) (w * Math.round(bounds.getMaxX() / w + 1));
//        final int y1 = (int) (h * Math.round(bounds.getMaxY() / h + 1));
//        g.setColor(grid_colour);
//        g.setStroke(grid_stroke);
//        for (int x = x0; x < x1;
//                x += w) {
//            for (int y = y0; y < y1;
//                    y += h) {
//                g.drawLine(x - 5, y, x + 5, y);
//                g.drawLine(x, y - 5, x, y + 5);
//            }
//        }
//    }
}
